package org.example.code.test2024.twelve;

import java.util.LinkedList;
import java.util.List;

/**
 * @Title: test1226
 * @Author yxy
 * @Package org.example.code.twelve
 * @Date 2024/12/26 15:00
 * @description:
 */
public class test1226 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        if (m > n) {
            return findMedianSortedArrays(nums2, nums1);
        }
        int left = 0, right = m;
        while (left <= right) {
            int p1 = (left + right) / 2;
            int p2 = (m + 1 + n) / 2 - p1;

            int maxLeft = p1 == 0 ? Integer.MIN_VALUE : nums1[p1 - 1];
            int minLeft = p1 == m ? Integer.MAX_VALUE : nums1[p1];

            int maxRight = p2 == 0 ? Integer.MIN_VALUE : nums2[p2 - 1];
            int minRight = p2 == n ? Integer.MAX_VALUE : nums2[p2];
            if (maxLeft <= minRight && maxRight <= minLeft) {
                if ((m + n) % 2 == 0) {
                    return (Math.max(maxLeft, maxRight) + Math.min(minLeft, minRight)) / 2.0;
                } else {
                    return Math.max(maxLeft, maxRight);
                }
            } else if (maxLeft > minRight) {
                right = p1 - 1;
            } else {
                left = p1 + 1;
            }
        }
        return 0;
    }

    public String getPermutation(int n, int k) {
        List<Integer> list = new LinkedList<>();
        for(int i=1;i<=n;i++){
            list.add(i);
        }
        k--;
        StringBuilder sb = new StringBuilder();
        int num = 1;
        for(int i=1;i<n;i++){
            num = num*i;
        }
        for(int i=n;i>0;i--){
            int index = k/num;
            sb.append(list.get(index));
            list.remove(list.get(index));
            k = k%num;
            if(i>1){
                num = num/(i-1);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        test1226 test = new test1226();
        int[] num1 = {1, 2};
        int[] num2 = {3, 4};
//        System.out.println(test.findMedianSortedArrays(num1, num2));
        System.out.println(test.getPermutation(4,9));
    }

}
